<?php
function redirect($page,$nsec)
{
  	echo "<meta http-equiv='refresh' content='".$nsec.";url=".$page."'>\n";
}

function newline($numline){
	for($i=1;$i<=$numline;$i++){
		echo "<br>";
	}
}

function get_template($template_name)
{
	$tn = $template_name.'.html';
	if (file_exists($tn))
	{
		$t = file_get_contents($tn);
		return "return <<<TMP\r\n<!-- START: $template_name -->\r\n{$t}\r\n<!-- END: $template_name -->\r\nTMP;\r\n";
	}
	else
	die("FATAL ERROR: File not found $template_name");
}

function trunc_url_title($url, $maxchars = 50)
{
  if (strlen($url) > $maxchars)
    return substr($url, 0, $maxchars - 5).' ... ';
  else
    return $url;
}

function trunc_url($url, $url_title)
{
  return '<a href="'.$url.'" target="_new" rel="nofollow">'.trunc_url_title($url_title).'</a>';
}

function format_bbcodes($text)
{
  global $lang;
  $search = array(
  '[b]','[/b]','[B]','[/B]','[i]','[/i]','[I]','[/I]','[u]','[/u]','[U]','[/U]');
  $replace = array(
  '<strong>','</strong>','<strong>','</strong>','<em>','</em>','<em>','</em>','<u>','</u>','<u>','</u>');
  
  $text = str_replace($search, $replace, $text);

  $search = array(
  '#\[img\](http|https|ftp)://(.*?)\[/img\]#i',
  '#\[email\](.*?)\[/email\]#i',
  '#\[email=(.*?)\](.*?)\[/email\]#i',  
  '#\[url=(http|https|ftp)://(.+?)\](.+?)\[/url\]#ie',
  '#\[url\](http|https|ftp)://(.+?)\[/url\]#ie',
  '#\[code\]#i',
  '#\[/code\]#i',
  '#\[quote\]#i',
  '#\[quote=(.*?)\]#i',
  '#\[/quote\]#i'
  );
  $replace = array(
  '<img src="\\1://\\2" alt="\\1://\\2">',
  '<a href="mailto:\\1">\\1</a>',
  '<a href="mailto:\\1">\\2</a>',
  'trunc_url(\'\\1://\\2\',\'\\3\')',
  'trunc_url(\'\\1://\\2\',\'\\1://\\2\')',
  '<div class="code"><b>Code : </b><br>',
  '</div>',
  '<div class="quote"><b> </b>',
  '<div class="quote"><b> \\1</b><br>',
  '</div>'
  );
  
  return preg_replace($search, $replace, $text);
}

function makeURLs($text)
{
  $search = array(
  '#(^|\s)(http|https|ftp)(://[^\s\[]+)#i',
  '#(^|\s)([a-z0-9-_.]+@[a-z0-9-.]+\.[a-z0-9-_.]+)#i'
  );
  $replace = array(
  '\\1[url]\\2\\3[/url]',
  '\\1[email]\\2[/email]'
  );
  return preg_replace($search, $replace, $text);
}

function fix_tabs_spaces($text)
{
  $search = array("\n", "\t", '  ', '  ');
  $replace = array('<br>', '&nbsp; &nbsp; ', '&nbsp; ', ' &nbsp;');
  return str_replace($search, $replace, $text);
}

?>